package sii.fb.emotion.computProbability;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sii.fb.emotion.Main;
import sii.fb.emotion.bean.Post;
import sii.fb.emotion.bean.User;


public class SupportMethod {
	Map<String, Float> initialData;

	public SupportMethod(){}
	
	public void computeInitialNumber(Map<Long, User> users, Map<Long, List<Post>> posts){
		computeUserNumber(users, posts);
	}
	

	//Emotion frequency in Facebook status updates.
	public void updateListPostEmotion(Post p, Float[] emotionPosts){
		float[] emotion = p.getEmotion();
		int cont = 0;
		for(int i=0; i<emotion.length; i++){
			if(emotion[i]>0){
				emotionPosts[i]++;
				cont++;
			}
		}
		if(cont==0){
			emotionPosts[10]++;
		}
	}

		
	//update emotionArray
	public void updateEmotionArray(float[] emotion, Float[] emotionPost){
		int cont = 0;
		for(int i=0; i<emotion.length; i++){
			if(emotion[i]>0){
				emotionPost[i]++;
				cont++;
			}
		}
		if(cont==0){
			emotionPost[10]++;
		}
	}
	
	public void normalizeListPostEm(Float[] emotionPosts, int nPost){
		for(int i = 0; i<emotionPosts.length; i++){
			emotionPosts[i] = (float)emotionPosts[i]/(float)nPost;
		}
		Main.print += Arrays.toString(emotionPosts)+ "\n";
	}
	
	private void computeUserNumber(Map<Long, User> users, Map<Long, List<Post>> posts){
		User u;
		initialData = new HashMap<String, Float>();
		float female = 0; float male = 0; float sumFemAge = 0; float sumMalAge= 0;
		float sumFemPost = 0; float sumMalPost = 0; float minFemPost = 101; float maxFemPost = 0;
		float minMalPost = 101; float maxMalPost = 0; float ageFpost = 0; float ageMpost = 0;
		for(Long i: users.keySet()){
			u = users.get(i);
			if(u.getGender().equals("female")) {
				female++;
				if(u.getBirtday()!=null){
					int age = 2014 - Integer.parseInt(new SimpleDateFormat("yyyy").format(u.getBirtday()));
					sumFemAge += age;
					ageFpost++;
				}
				int post = posts.get(i).size();
				sumFemPost += post;
				if(post<minFemPost &&post>0){
					minFemPost = post;
				}
				if(post>maxFemPost){
					maxFemPost = post;
				}
			}
			else {
				male ++;
				if(u.getBirtday()!=null){
					int age = 2014 - Integer.parseInt(new SimpleDateFormat("yyyy").format(u.getBirtday()));
					sumMalAge += age;
					ageMpost++;
				}
				int post = posts.get(i).size();
				sumMalPost += post;
				if(post<minMalPost && post>0){
					minMalPost = post;
				}
				if(post>maxMalPost){
					maxMalPost = post;
				}
			}
							
		}
		initialData.put("female",female);
		initialData.put("male",male);
		initialData.put("avgFemAge",sumFemAge/ageFpost);
		initialData.put("avgMalAge",sumMalAge/ageMpost);
		initialData.put("sumFemPost",sumFemPost);
		initialData.put("sumMalPost",sumMalPost);
		initialData.put("minFemPost", minFemPost);
		initialData.put("minMalPost", minMalPost);
		initialData.put("maxFemPost", maxFemPost);
		initialData.put("maxMalPost", maxMalPost);
		initialData.put("avgFemPost", sumFemPost/female);
		initialData.put("avgMalPost", sumMalPost/male);
		Main.print += "----------INIATIAL DATA----------\n"+initialData.toString()+"\n";

		
	}
	
	
	String convertEmotion(int i){
		switch(i){
		case 10: return "No emotion";
		case 0: return "Positive";
		case 1: return "Negative";
		case 2: return "Anger";
		case 3: return "Anticipation";
		case 4: return "Disgust";
		case 5: return "Fear";
		case 6: return "Joy";
		case 7: return "Sadness";
		case 8: return "Surprise";
		case 9: return "Trust";
		}
		return null;
	}
}
